package cn.fl.project.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.condition.PrjTransSettingQueryCondition;
import cn.fl.project.service.IPrjTransSettingService;
import cn.fl.project.vo.PrjTransSettingVO;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.service.ISysOrgService;
import cn.fl.system.vo.SysOrgVO;
import com.alibaba.fastjson.JSONObject;
import net.sf.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 项目转办组织架构设置表 控制层
 */
@Controller
@RequestMapping("/project/prjTransSetting")
public class PrjTransSettingController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(PrjTransSettingController.class);

    /**
     * PrjTransSetting 服务层BEAN
     */
    @Autowired
    private IPrjTransSettingService prjTransSettingService;

    /**
     * SysOrg 服务层BEAN
     */
    @Autowired
    private ISysOrgService sysOrgService;

    /**
     * 到列表页面
     */
    @RequestMapping(value = "/toListPage.do", method = {RequestMethod.GET})
    public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toListPage", "加载PrjTransSetting列表页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("project/prjTransSetting/prjTransSettingList");
//        //公司级别
//        List<KeyValueVO> searchSelectOrgTypCdList = this.sysOrgService.searchSysOrgTypCdList();
//        if (searchSelectOrgTypCdList != null) {
//            mav.addObject("searchSelectOrgTypCdList", searchSelectOrgTypCdList);
//        }
        return mav;
    }

    /**
     * 到新增页面
     */
    @RequestMapping(value = "/toAddPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toAddPage", "加载PrjTransSetting Add页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("project/prjTransSetting/prjTransSettingModify");
        //this.prepare(request,mav);
        return mav;
    }

    /**
     * 到修改页面
     */
    @RequestMapping(value = "/toModifyPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toModifyPage", "加载PrjTransSetting Modify页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("project/prjTransSetting/prjTransSettingModify");
        //this.prepare(request,mav);

        String id = request.getParameter("id");
        PrjTransSettingVO prjTransSettingVO = this.prjTransSettingService.selectById(Long.valueOf(id));
        mav.addObject("prjTransSettingVO", prjTransSettingVO);
        return mav;
    }

    /**
     * 根据名称查询转出组织架构
     *
     * @param request
     * @param response
     * @return
     * @throws BaseException
     */

    @RequestMapping(value = "/getByResNm.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object getByResNm(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        // 获取转出架构参数
        String outOrgNm = request.getParameter("outOrgNm");
        // 获取转入架构参数
        String inOrgNm = request.getParameter("inOrgNm");
        BaseLogUtils.info(logger, "getByResNm", "加载资源树.", CurrentThreadContext.getCurrentUserName());

        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
        JSONArray jsonArray = null;
        // JSONObject jsonObject = null;
        try {
            if (StringUtils.isNotBlank(outOrgNm)) {
                jsonArray = sysOrgService.searchOrgTreeByOrgNm(outOrgNm, CurrentThreadContext.getCurrentSysCd());
                // jsonObject = jsonArray.getJSONObject(0);
            }
            if (StringUtils.isNotBlank(inOrgNm)) {
                jsonArray = sysOrgService.searchOrgTreeByOrgNm(inOrgNm, CurrentThreadContext.getCurrentSysCd());
                // jsonObject = jsonArray.getJSONObject(0);
            }
            if (jsonArray == null) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
                BaseLogUtils.info(logger, "getByResNm", "获取资源数据不存在.", CurrentThreadContext.getCurrentUserName());
            } else {
                BaseLogUtils.info(logger, "getByResNm", "加载资源树成功.", CurrentThreadContext.getCurrentUserName());
            }
            result.setData(jsonArray);
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
            BaseLogUtils.info(logger, "getByResNm", "获取资源数据失败.ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "getByResNm", "获取资源数据失败,系统错误.ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    //加载数据字典数据、传值等 ？
    public void prepare(HttpServletRequest request, ModelAndView mav) {

    }

    /**
     * 初始化列表点一次查一次
     */
    @RequestMapping(value = "/initOrgList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initOrgList(HttpServletRequest request, HttpServletResponse response) {
        try {
            //JSONArray jsonArray = this.sysOrgService.searchList(SysOrgConstant.SYS_ORG_CD_PID_INIT);
            JSONArray jsonArray = new JSONArray();
            jsonArray = this.sysOrgService.searchOrgTreeByCurrentUser(CurrentThreadContext.getCurrentBuOrgCd(), CurrentThreadContext.getFlServiceSysCd());
            BaseLogUtils.info(logger, "initList", "初始化组织架构列表页面.", CurrentThreadContext.getCurrentUserName());
            return jsonArray;
        } catch (Exception e) {
            BaseLogUtils.error(logger, "initList", "初始化组织架构列表页面失败.系统错误:" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return null;
    }

    /**
     * 得到一条记录
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getOrgById.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object getOrgById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        String id = request.getParameter("id");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
        if (StringUtils.isEmpty(id)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
            BaseLogUtils.info(logger, "getById", "获取组织架构数据,主键参数为空！", CurrentThreadContext.getCurrentUserName());
            return result;
        }

        try {
            SysOrgVO vo = this.sysOrgService.selectById(Long.valueOf(id));
            if (null == vo) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
                BaseLogUtils.info(logger, "getById", "获取组织架构数据不存在,ID:" + id, CurrentThreadContext.getCurrentUserName());
            }
            result.setData(vo);
            BaseLogUtils.info(logger, "getById", "获取组织架构数据成功,ID:" + id, CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
            BaseLogUtils.info(logger, "getById", "获取组织架构数据失败,ID:" + id + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "getById", "获取组织架构数据失败,系统错误.ID:" + id + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 通过角色id得到资源树
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getCheckedOrgByOutOrgId.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object getCheckedOrgByOutOrgId(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        Long outOrgId = Long.parseLong(request.getParameter("outOrgId"));
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
        if (StringUtils.isEmpty(outOrgId)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_AUTH_ROLE_ID_NOT_EMPTY);
            BaseLogUtils.info(logger, "getByRoleId", "通过角色ID获取资源树数据,角色ID参数为空！", CurrentThreadContext.getCurrentUserName());
            return result;
        }

        try {
            PrjTransSettingQueryCondition qc = new PrjTransSettingQueryCondition();
            qc.setOutOrgId(outOrgId);
            // 获取配置好的可转办的组织架构集合
            List<PrjTransSettingVO> inOrgList = prjTransSettingService.searchList(qc);
            // 系统架构的集合
            List<SysOrgVO> orgVOList = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(inOrgList)) {
                SysOrgVO orgVO = null;
                for (PrjTransSettingVO vo : inOrgList) {
                    orgVO = sysOrgService.selectById(vo.getInOrgId());
                    orgVOList.add(orgVO);
                }
            }
            // 获取数据集合
            JSONArray sysOrgArray = prjTransSettingService.sysOrgList2JsonArray(orgVOList);

            net.sf.json.JSONObject jsonObject = new net.sf.json.JSONObject();
            jsonObject.put("data", sysOrgArray);
            if (jsonObject == null) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
                BaseLogUtils.info(logger, "getCheckedOrgByOutOrgId", "通过转出组织架构获取转入组织架构不存在,outOrgId:" + outOrgId, CurrentThreadContext.getCurrentUserName());
            }
            result.setData(jsonObject);
            BaseLogUtils.info(logger, "getCheckedOrgByOutOrgId", "通过转出组织架构获取转入组织架构成功,outOrgId:" + outOrgId, CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
            BaseLogUtils.info(logger, "getCheckedOrgByOutOrgId", "通过转出组织架构获取转入组织架构失败,outOrgId:" + outOrgId + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "getCheckedOrgByOutOrgId", "通过转出组织架构获取转入组织架构失败,系统错误.outOrgId:" + outOrgId + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 初始化列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/initList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initPrjTransSettingList(HttpServletRequest request, HttpServletResponse response) {
        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        Pager page = new Pager();
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }

        if (StringUtils.isNotEmpty(pageSize)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }

        PrjTransSettingQueryCondition qc = new PrjTransSettingQueryCondition();
        //TODO 设置查询条件？
        String searchName = request.getParameter("searchName");
        if (StringUtils.isNotEmpty(searchName)) {

        }
        page.setCondition(qc);

        BaseLogUtils.info(logger, "initPrjTransSettingList", "初始化PrjTransSetting列表页面数据.DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
        Pager pager = this.prjTransSettingService.searchListPage(page);
        List<PrjTransSettingVO> list = (List<PrjTransSettingVO>) pager.getResultList();
        ListPageVO<PrjTransSettingVO> listPage = new ListPageVO<PrjTransSettingVO>(list, pager.getTotalCount());
        return listPage;
    }

    /**
     * 删除数据
     */
    @RequestMapping(value = "/removeByIds.do", method = {RequestMethod.POST})
    @ResponseBody
    public DataResultVO removePrjTransSettingByIds(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        String ids = request.getParameter("ids");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
        if (StringUtils.isEmpty(ids)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
            BaseLogUtils.info(logger, "removePrjTransSettingByIds", "删除PrjTransSetting数据,主键参数为空!", CurrentThreadContext.getCurrentUserName());
            return result;
        }

        List<Long> idsList = new ArrayList<Long>();
        JSONArray idsArray = JSONArray.fromObject(ids);
        for (Object object : idsArray) {
            idsList.add(Long.valueOf(object.toString()));
        }

        try {
            boolean success = this.prjTransSettingService.removeByIds(idsList);
            if (!success) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_DELETE_FAIL);
                BaseLogUtils.info(logger, "removePrjTransSettingByIds", "删除PrjTransSetting数据失败,ID:" + idsList.toString(), CurrentThreadContext.getCurrentUserName());
            }
            BaseLogUtils.info(logger, "removePrjTransSettingByIds", "删除PrjTransSetting数据成功,ID:" + idsList.toString(), CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.info(logger, "removePrjTransSettingByIds", "删除PrjTransSetting数据失败,ID:" + idsList.toString() + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "removePrjTransSettingByIds", "删除PrjTransSetting数据失败,系统错误.ID:" + idsList.toString() + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 得到一条记录
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getById.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object getPrjTransSettingById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        String id = request.getParameter("id");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
        if (StringUtils.isEmpty(id)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
            BaseLogUtils.info(logger, "getPrjTransSettingById", "获取PrjTransSetting数据,主键参数为空.", CurrentThreadContext.getCurrentUserName());
            return result;
        }

        try {
            PrjTransSettingVO vo = this.prjTransSettingService.selectById(Long.valueOf(id));
            if (null == vo) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
                BaseLogUtils.info(logger, "getPrjTransSettingById", "获取PrjTransSetting数据不存在,ID:" + id, CurrentThreadContext.getCurrentUserName());
            }
            result.setData(vo);
            BaseLogUtils.info(logger, "getPrjTransSettingById", "获取PrjTransSetting数据成功,ID:" + id, CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.info(logger, "getPrjTransSettingById", "获取PrjTransSetting数据失败,ID:" + id + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "getPrjTransSettingById", "获取PrjTransSetting数据失败,系统错误.ID:" + id + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object addPrjTransSetting(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
        Map<String, List<PrjTransSettingVO>> voMap = null;//获取参数
        try {
            voMap = this.getRequestParamForPrjTransSettingAndSearch(request);//获取参数
            Long id = 0l;
            // 新增的转办配置集合
            List<PrjTransSettingVO> addList = voMap.get("add");
            for (PrjTransSettingVO entity : addList) {
                this.prjTransSettingService.add(entity);
            }
            // 已选择的转办配置集合
            List<PrjTransSettingVO> selectedVOs = voMap.get("selectedVOs");
            for (PrjTransSettingVO entity : selectedVOs) {
                this.prjTransSettingService.prjTransSettingUpdateIsDel(entity);
            }
            // 未选择的转办配置集合
            List<PrjTransSettingVO> unselectedVOS = voMap.get("unselectedVOS");
            for (PrjTransSettingVO entity : unselectedVOS) {
                this.prjTransSettingService.prjTransSettingUpdateIsDel(entity);
            }
            result.setData(1l);
            BaseLogUtils.info(logger, "addPrjTransSetting", "新增PrjTransSetting数据成功,ID:" + id + ".DATA:" + JSONObject.toJSONString(voMap), CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.info(logger, "addPrjTransSetting", "新增PrjTransSetting数据失败.DATA:" + JSONObject.toJSONString(voMap) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.error(logger, "addPrjTransSetting", "新增PrjTransSetting数据失败,系统错误.DATA:" + JSONObject.toJSONString(voMap) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

//    /**
//     * 修改
//     */
//    @RequestMapping(value = "/modify.do", method = {RequestMethod.POST})
//    @ResponseBody
//    public Object modifyPrjTransSetting(HttpServletRequest request, HttpServletResponse response) throws BaseException {
//        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
//        List<PrjTransSettingVO> vos = null;//获取参数
//        try {
//            vos = this.getRequestParamForPrjTransSettingAndSearch(request);//获取参数
//            Long id = 0l;
//            boolean success = true;
//            for (PrjTransSettingVO entity : vos) {
//                if (entity.getId() == null) {
//                    result.setSuccess(BaseConstant.IS_YESNO_NO);
//                    result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
//                    BaseLogUtils.info(logger, "modifyPrjTransSetting", "修改PrjTransSetting信息,主键参数为空！.", CurrentThreadContext.getCurrentUserName());
//                    return result;
//                }
//                success = this.prjTransSettingService.modify(entity);
//            }
//            if (!success) {
//                result.setSuccess(BaseConstant.IS_YESNO_NO);
//                result.setInfo(SysLogConstant.LOG_CMM_UPDATE_FAIL);
//                BaseLogUtils.info(logger, "modifyPrjTransSetting", "修改PrjTransSetting数据失败.DATA:" + JSONObject.toJSONString(vos), CurrentThreadContext.getCurrentUserName());
//                return result;
//            }
//            BaseLogUtils.info(logger, "modifyPrjTransSetting", "修改PrjTransSetting数据成功.DATA:" + JSONObject.toJSONString(vos), CurrentThreadContext.getCurrentUserName());
//        } catch (BaseException ex) {
//            result.setSuccess(BaseConstant.IS_YESNO_NO);
//            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
//            BaseLogUtils.info(logger, "modifyPrjTransSetting", "修改PrjTransSetting数据失败.DATA:" + JSONObject.toJSONString(vos) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
//        } catch (Exception ex) {
//            result.setSuccess(BaseConstant.IS_YESNO_NO);
//            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
//            BaseLogUtils.error(logger, "modifyPrjTransSetting", "修改PrjTransSetting数据失败,系统错误.DATA:" + JSONObject.toJSONString(vos) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
//        }
//        return result;
//    }

    /**
     * 获取请求参数并处理成三种：①条件为(inOrgId, outOrgId)：查出来 1->0;没查出来 add ②条件为(outOrgId)：其余的 0->1
     *
     * @param request 请求
     * @return
     */
    private Map<String, List<PrjTransSettingVO>> getRequestParamForPrjTransSettingAndSearch(HttpServletRequest request) throws BaseException {
        Map<String, List<PrjTransSettingVO>> voMap = new HashMap<String, List<PrjTransSettingVO>>();

        // 获取参数
        String id = request.getParameter("id");//主键
        Long outOrgId = Long.parseLong(request.getParameter("outOrgId"));// 转出组织架构ID
        String outOrgCd = request.getParameter("outOrgCd");// 转出组织架构代码
        String outOrgNm = request.getParameter("outOrgNm");// 转出组织架构名称
        String[] inOrgArr = request.getParameterValues("inOrgArr[]");// 转入组织架构的数组
        Long inOrgId = 0l;// 转入组织架构ID
        String inOrgCd = "";// 转入组织架构代码
        String inOrgNm = "";// 转入组织架构名称
        // 存放json对象的list集合
        List<net.sf.json.JSONObject> inOrgList = null;
        if (inOrgArr != null && inOrgArr.length > 0) {
            // 获取json数组
            JSONArray array = JSONArray.fromObject(inOrgArr[0]);
            inOrgList = new ArrayList<net.sf.json.JSONObject>();
            // 遍历json数组
            for (int i = 0; i < array.size(); i++) {
                // 数组转成json对象
                net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(array.get(i));
                JSONArray children = JSONArray.fromObject(jsonObject.get("children"));
                // 只存最小的组织架构单元
                if (children.size() == 0)
                    inOrgList.add(jsonObject);
            }
        }

        // add的集合
        List<PrjTransSettingVO> addVOs = new ArrayList<PrjTransSettingVO>();
        List<PrjTransSettingVO> selectedVOs = new ArrayList<PrjTransSettingVO>();
        if (CollectionUtils.isNotEmpty(inOrgList)) {
            // 循环转入的组织架构json对象集合
            for (net.sf.json.JSONObject json : inOrgList) {
                if (json.containsKey("id")) {
                    inOrgId = Long.parseLong(json.getString("id"));
                }
                if (json.containsKey("orgCd")) {
                    inOrgCd = json.getString("orgCd");
                }
                if (json.containsKey("orgDispNm")) {
                    inOrgNm = json.getString("orgDispNm");
                }
                PrjTransSettingVO vo = prjTransSettingService.searchPrjTransSettingVOByInOrgIdAndOutOrgId(inOrgId, outOrgId);
                if (vo == null) {
                    vo = new PrjTransSettingVO();
                    vo.setOutOrgId(outOrgId); // 转出组织架构ID
                    vo.setOutOrgCd(outOrgCd); // 转出组织架构代码
                    vo.setOutOrgNm(outOrgNm); // 转出组织架构名称
                    vo.setInOrgId(inOrgId); // 转入组织架构ID
                    vo.setInOrgCd(inOrgCd); // 转入组织架构代码
                    vo.setInOrgNm(inOrgNm); // 转入组织架构名称
                    Date tm = new Date();
                    vo.setCrtTm(tm); // 创建时间
                    vo.setCrtUsrId(CurrentThreadContext.getCurrentUserId()); // 创建人id
                    addVOs.add(vo);
                } else {
                    vo.setIsDel(0);
                    vo.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
                    vo.setMdfTm(new Date());
                    selectedVOs.add(vo);
                }
            }
        }
        voMap.put("add", addVOs);
        voMap.put("selectedVOs", selectedVOs);
        PrjTransSettingQueryCondition qc = new PrjTransSettingQueryCondition();
        qc.setOutOrgId(outOrgId);
        // 查出全部当前转出机构在数据库保存的项目转办配置对象
        List<PrjTransSettingVO> voList = prjTransSettingService.selectPrjTransSettingListByOutOrgId(qc);
        voList.removeAll(selectedVOs);
        List<PrjTransSettingVO> unselectedVOS = new ArrayList<PrjTransSettingVO>();
        // 去掉选中的就是未选中的，改变删除状态
        for (PrjTransSettingVO vo : voList) {
            vo.setIsDel(1);
            vo.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
            vo.setMdfTm(new Date());
            unselectedVOS.add(vo);
        }
        voMap.put("unselectedVOS", unselectedVOS);
        return voMap;
    }
}

